home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / applic / ntp / acts.zoo / dial.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-14  |  5.8 KB  |  180 lines

  1. int dial()
  2. {
  3. /*
  4.         this subroutine dials a hayes modem connected
  5.         to the com port using calls to wrtbuf and rdbuf.
  6.  
  7.         if the first character in the dialing sequence (after
  8.         the ATD) is M (or m), then "manual" dialing is assumed.
  9.         in that case, just return and wait for received message.
  10.  
  11.         if echo is set to zero by the configuration file, then
  12.         echoes are not expected from the modem.  if echo is not zero
  13.         then echoes are expected and a fatal error exists if
  14.         echoes are not received within the time out period.
  15.  
  16.         although this subroutine does not access the hardware
  17.         directly it does depend on the method used.  this is because
  18.         the non-bios version of the program is much faster than the
  19.         bios version and some timeouts must therefore be different.
  20.         also the non-bios version is fast enough to get think that the
  21.         trailing digit or two of the telephone number are actually a
  22.         modem response to the dialing
  23. */
  24. #include "nbstime.h"
  25. #include <stdio.h>
  26. #ifdef IBMPC
  27. #include <dos.h>
  28. #endif
  29. void wrtbuf(),wait();
  30. int rdbuf();
  31. char *ptr;
  32. char ans[30],cc;
  33. char ie1 = 'K';
  34. char ie2 = '\n';
  35. char ie3 = '\r';
  36. char ie4 = 'Y';        /* response is BUSY */
  37. char ie5 = 'T';        /* response is CONNECT */
  38. char ie6 = 'R';        /* response is NO ANSWER or NO CARRIER */
  39. char ie7 =  -120;
  40. int j,k,kk,stat;
  41. #ifdef IBMPC
  42. #ifdef BIOS
  43. int tmo  =  -3;     /* time-out limit, roughly 5 seconds */
  44. int ttmo =  -40;    /* wait for number to be dialed */
  45. #endif
  46. #ifndef BIOS
  47. int tmo  =  -15;    /* time-out limit, roughly 3 per second */
  48. int ttmo = -400;    /* bigger number for non-bios use */
  49. #endif
  50. #endif
  51. #ifdef SUN
  52. int tmo = -10;
  53. int ttmo= -200;     /* this is a LONG time-out. */
  54. #endif
  55. extern char number[30];
  56. extern int echo;
  57. extern int debug;
  58.         if( (number[3] == 'm') || (number[3] == 'M') )
  59.         {
  60.         printf("\n Ready for manual dialing. Please dial now. \n enter")
  61. ;
  62.         printf(" y <return> if connected, n <return> otherwise\n");
  63.         scanf("%c",&cc);
  64.         if( (cc == 'y') || (cc == 'Y')) return(1);
  65.         return(0);
  66.         }
  67.         ptr="ATZ\r";
  68.         wrtbuf(ptr);
  69.         if( (rdbuf(ans,ie1,ie7,ie7,tmo) == 0) && (echo != 0) )
  70.         {
  71.         printf("\n No response from modem. -1\n");
  72.         abort();
  73.         }
  74.         if( (rdbuf(ans,ie2,ie7,ie7,tmo) == 0) && (echo != 0) )
  75.         {
  76.         printf("\n No response from modem. -2\n");
  77.         abort();
  78.         }
  79.         wait();
  80. /*
  81.         set: echo on, full duplex, speaker on, basic word responses on
  82. */
  83.         ptr="AT E1 M1 Q0 V1\r";
  84.         wrtbuf(ptr);
  85.         if( (rdbuf(ans,ie1,ie7,ie7,tmo) == 0) && (echo !=0) )
  86.         {
  87.         printf("\n No response from modem. -3\n");
  88.         abort();
  89.         }
  90.         wait();
  91.         wrtbuf(number);
  92.         if( (rdbuf(ans,ie3,ie7,ie7,tmo) == 0) && (echo !=0) )
  93.         {
  94.         printf("\n No response from modem. -4\n");
  95.         abort();
  96.         }
  97. /*
  98.     now wait for response from modem.  there are several
  99.     possibilities as follows:
  100.     1.  modem does not response at all.  this is almost
  101.         certainly an error -- flag immediately if echo
  102.         mode enabled. otherwise parse response.
  103.  
  104.     2.  read terminates on R from RINGING -- only some modems
  105.         can do this -- go back and read some more. do not
  106.         reset buffer pointer here since response is only
  107.         partial.
  108.  
  109.     3.  read terminates on R from NO ANSWER or NO CARRIER --
  110.         that's all folks.
  111.  
  112.     4.  read terminates on T from CONNECT -- the kind of 
  113.         stuff we like to see.
  114.  
  115.     5.  read terminates on T from AT or ATDT -- throw
  116.         it away and read some more.  this should only
  117.         happen on SUN machines where the reads are buffered
  118.         on IBMPC where the input port is not buffered the
  119.         ATDT string should have been overwritten by the
  120.         digits of the number.
  121.     the variable stat is 0 until we are finished and is
  122.     1 when the final modem status has been received
  123.     kk points to the origin of the buffer for the current
  124.     read.  it is zero unless a partial answer has been
  125.     received (NO CAR for example).
  126. */
  127.     stat=0;
  128.     kk=0;
  129.     do
  130.     {
  131. /*
  132.        read some stuff, abort if timeout and no response
  133.        (modem should have given status of call).
  134.  
  135.        when rdbuf returns, k points to terminator relative to
  136.        origin which is kk.
  137. */
  138.            if( (k=rdbuf(&ans[kk],ie4,ie5,ie6,ttmo)) == 0 )
  139.               {
  140.               printf("\n No response from modem. -5\n");
  141.               abort();
  142.               }
  143. /*
  144.        if we read more than one character, and 
  145.        if end of message is ER, we have final modem status.
  146.        answer is either NO ANSWER or NO CARRIER. bad news 
  147.        either way but we are finished.
  148. */
  149.            if( (k  > 1) && (ans[kk+k-1] == 'R') && (ans[kk+k-2] == 'E') )
  150.           stat=1;
  151. /*
  152.        if we read more than one character and
  153.        if end of message is CT we have final modem status.
  154.        answer is CONNECT (possibly CONNECT 1200).
  155. */
  156.        if( (k > 1) && (ans[kk+k-1] == 'T') && (ans[kk+k-2] == 'C') )
  157.           stat=1;
  158. /*
  159.        if we terminated on an R and it is the first character, then
  160.        it is the R of RINGING. if we read more than one character
  161.        and we ended with an R and the previous character was not E,
  162.        then we are in the midst of NO CARRIER  -- keep on reading.
  163. */
  164.        if( (kk+k > 1) && (ans[kk+k-1] == 'R') && (ans[kk+k-2] != 'E') )
  165.           kk += k;
  166.        if( (kk+k == 1) && (ans[0] == 'R') )
  167.           kk += k;
  168.     } while (stat == 0);
  169. /*
  170.     since modem response was set to words, skip leading
  171.     extraneous stuff including remnants of number and possible
  172.     leading newline
  173. */
  174.     for(j=0; ( (j < k+kk) && (ans[j] < 'A')) ; j++)  ;
  175.         printf("\n modem answer= %s\n",&ans[j]);
  176.         stat=0;
  177.         for(k=j; (cc=ans[k]) != 0; k++) if(cc == 'T') stat=1;
  178.         return(stat);
  179. }
  180.